package leetcode.code1739;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int minimumBoxes(int n) {
		List<Long> s = new ArrayList<>();
		List<Long> ss = new ArrayList<>();
		long num = 1, sum = 0, sumsum = 0;
		while (sumsum < n) {
			sum += num;
			sumsum += sum;
			num++;
			s.add(sum);
			ss.add(sumsum);
		}
		H.print(s);
		H.print(ss);
		System.out.println(s.size());
		System.out.println(ss.size());
		int size = ss.size();
		long toto = ss.get(size - 1);
		long to = s.get(size - 1);
		int l = 1, r = size;
		int ans = 0;
		while (l <= r) {
			int m = ((r - l) >> 1) + l;
			long mm = ss.get(m - 1);
			long cur = toto - (to - mm);
			if (cur >= n) {
				ans = m;
				r = m - 1;
			} else {
				l = m + 1;
			}
		}
		return (int) (s.get(size - 2) + ans);
	}

	@Override
	public void debug4() {
		int ans = this.minimumBoxes(1047147);
		System.out.println(ans);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
